package io.renren.modules.efinance.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.PasswordUtil;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.efinance.entity.AccountEntity;
import io.renren.modules.efinance.entity.AccountflowEntity;
import io.renren.modules.efinance.entity.BankcardEntity;
import io.renren.modules.efinance.service.AccountService;
import io.renren.modules.efinance.service.AccountflowService;
import io.renren.modules.efinance.service.BankcardService;
import io.renren.modules.efinance.vo.RechargeVo;
import io.renren.modules.efinance.vo.TransferVo;
import io.renren.modules.efinance.vo.WithdrawVo;
import io.renren.modules.sys.entity.SysUserEntity;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * @author lhf
 * @email i@lhf.com
 * @date 2022-06-24 17:18:43
 */
@RestController
@RequestMapping("efinance/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("efinance:account:list")
    public R list(@RequestParam Map<String, Object> params) {
        SysUserEntity sysUser = ShiroUtils.getUserEntity();
        params.put("account_name",sysUser.getUsername());
        PageUtils page = accountService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{accountId}")
    // @RequiresPermissions("efinance:account:info")
    public R info(@PathVariable("accountId") String accountId) {
        AccountEntity account = accountService.getById(accountId);

        return R.ok().put("account", account);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("efinance:account:save")
    public R save(@RequestBody AccountEntity account) {
        accountService.save(account);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("efinance:account:update")
    public R update(@RequestBody AccountEntity account) {
        accountService.updateById(account);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("efinance:account:delete")
    public R delete(@RequestBody String[] accountIds) {
        accountService.removeByIds(Arrays.asList(accountIds));

        return R.ok();
    }

    /**
     * 充值：根据用户名获取当前用户，根据银行卡号获取银行卡信息，输入密码充值
     * @return
     */
    @RequestMapping("/recharge")
    public R recharge(@RequestBody RechargeVo rechargeVo){
        accountService.recharge(rechargeVo);
        return R.ok();
    }

    /**
     * 提现
     */
    @RequestMapping("/withdraw")
    public R withdraw(@RequestBody WithdrawVo withdrawVo){
        accountService.withdraw(withdrawVo);
        return R.ok();
    }

    /**
     * 虚拟转账
     */
    @RequestMapping("/transfer")
    public R transfer(@RequestBody TransferVo transferVo){
        accountService.transfer(transferVo);
        return R.ok();
    }

    /**
     * 注册功能
     * 用户注册成功，默认有账户权限
     */
    @PostMapping("/register")
    public R register(@RequestBody AccountEntity entity) {
        if (entity.getAccountId().length() == 15 || entity.getAccountId().length() == 18) {
            String accountId = entity.getAccountId();
            String frontId = accountId.substring(0, accountId.length() - 1);
            String lastID = "" + accountId.charAt(accountId.length() - 1);
            if ("X".equals(lastID)||"x".equals(lastID)){
                lastID=""+10;
            }
            try {
                Long.parseLong(frontId);
            } catch (NumberFormatException e) {
                return R.error("身份证不符合规格");
            }
            accountId = frontId + lastID;
            entity.setAccountId(accountId);
            if (entity.getAccountPhone().length()==11){
                try {
                    Long.parseLong(entity.getAccountPhone());
                } catch (NumberFormatException e) {
                    return R.error("手机号输入错误，请重新输入");
                }
                accountService.insert(entity);
            }else {
                return R.error("手机号输入错误，请重新输入");
            }
            return R.ok("注册成功！");
        } else {
            return R.error("身份证号不符合规格");
        }

    }


    /**
     * 登录
     *
     * @param request
     * @param account
     * @return
     */
//    @PostMapping("/login")
//    public R login(HttpServletRequest request, @RequestBody AccountEntity account) {
//
//        //1.比对数据库与前端的用户名是否一致
//        LambdaQueryWrapper<AccountEntity> queryWrapper = new LambdaQueryWrapper<>();
//        String accountName = account.getAccountName();
//        queryWrapper.eq(AccountEntity::getAccountName, accountName);
//        AccountEntity acc = accountService.getOne(queryWrapper);
//        if (acc == null) {
//            return R.error("登陆失败");
//        }
//        String salt = acc.getSalt();
//        String oldPassword = account.getAccountPassword();
//        String md5Password = PasswordUtil.getMD5Password(oldPassword, salt);
//        //如果一致获取此用户名下的盐值，进行加密后在进行用户名密码比对如果一致将用户名/手机号存入session中
//        if (!acc.getAccountPassword().equals(md5Password)) {
//            return R.error("登录失败");
//        }
//        if (acc.getAccountStatus() != 0) {
//            return R.error("账号已销户");
//        }
//        if (acc.getAccountStatus() != 1) {
//            return R.error("账号已冻结");
//        }
//        request.getSession().setAttribute("acc", acc.getAccountName());
//
//        return R.ok("登陆成功");
//    }


    /**
     * 获取性别，男1 女0
     *
     * @param accountId
     * @return
     */
    private Integer getSex(String accountId) {

        Integer sex = null;
        if (accountId.length() == 15) {
            sex = (int) accountId.charAt(14);
            if (sex % 2 == 0) {
                sex = 0;
            } else {
                sex = 1;
            }

        } else if (accountId.length() == 18) {
            sex = (int) accountId.charAt(17);
            if (sex % 2 == 0) {
                sex = 0;
            } else {
                sex = 1;
            }
        }
        return sex;
    }

    /**
     * 获取生日
     *
     * @param accountId
     * @return
     */
    public String getBirthday(String accountId) {
        String birthday = null;
        if (accountId.length() == 15) {
            String substring = accountId.substring(6, 12);
            birthday = "19" + substring;
            birthday = toBirthdayString(birthday);
        } else if (accountId.length() == 18) {
            birthday = accountId.substring(6, 14);
            birthday = toBirthdayString(birthday);
        }
        return birthday;
    }

    /**
     * 生日的格式转化
     *
     * @param birthday
     * @return
     */
    public String toBirthdayString(String birthday) {
        String year = birthday.substring(0, 4) + "年";
        String month = birthday.substring(4, 6) + "月";
        String day = birthday.substring(6, 8) + "日";
        birthday = year + month + day;
        return birthday;
    }
}
